package com.dylan.springcloud.common.vo;

import com.dylan.springcloud.common.util.DynamicObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Dylan on 2018/8/14.
 */
public class SimpleTip {
    public static final int SUCCESS_CODE = 0;
    public static final int FAIL_CODE = 1;

    private int code = SUCCESS_CODE;
    private String message = "";
    private Map<String, Object> info;

    // 不推荐使用,此构造函数用于动态创建对象
    public SimpleTip() {}

    private SimpleTip(int code, String message) {
        this.code = code;
        this.message = (message == null ? "" : message.trim());
    }

    public static SimpleTip success() {
        return new SimpleTip(SUCCESS_CODE, "");
    }

    public static SimpleTip success(String message) {
        return new SimpleTip(SUCCESS_CODE, message);
    }

    public static SimpleTip fail(String message) {
        return new SimpleTip(FAIL_CODE, message);
    }

    public static SimpleTip fail(int code, String message) {
        return new SimpleTip(code, message);
    }

    public SimpleTip putInfo(String name, Object value) {
        if (info == null) {
            info = new HashMap<>();
        }
        info.put(name, value);
        return this;
    }

    public SimpleTip putInfoAll(Map<String, Object> infos) {
        if (info == null) {
            info = new HashMap<>();
        }
        info.putAll(infos);
        return this;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, Object> getInfo() {
        return info;
    }

    public SimpleTip bindInfo() {
        return DynamicObject.createBuilder(SimpleTip.class)
                .addFieldValueInMap(info)
                .setFieldValue("message", this.message)
                .setFieldValue("code", this.code)
                .build();
    }
}
